/*
 *  rgs_label.c
 *  RGS
 *
 *  Copyright 2008 Ivan Safrin.
 *
 *  This code is distributed under the LGPL license.
 *  You should have a copy of LGPL.txt distributed
 *  with the project, but if not you can get a copy
 *  at http://www.gnu.org/licenses/
 *
 */

#include "rgs_label.h"
#include "BFont.h"
#include "rgs_core.h"
#include "SDL.h"

RGS_Label *RGS_CreateLabel(RGS_Screen *screen, char *caption, int fontType, int x, int y, int color) {
	RGS_Label *newLabel;
	newLabel = (RGS_Label*)malloc(sizeof(RGS_Label));
	memset(newLabel, 0, sizeof(RGS_Label));
	RGS_InitializeObject(&newLabel->object, x, y);
	RGS_AddScreenObject(screen, (RGS_Object*)newLabel);
	newLabel->object.Render = RGS_RenderLabel;
	
	switch(fontType) {
		case RGS_FONT_SMALL:
			newLabel->font = g_rgs->smallFont;
		break;
		case RGS_FONT_BIG:
			newLabel->font = g_rgs->bigFont;
		break;
		default:
			newLabel->font = g_rgs->smallFont;
		break;
	}
	
	RGS_SetLabelText(newLabel, caption);

	newLabel->color = RGS_SplitRGB(color);
	
	return newLabel;
}

void RGS_RenderLabel(SDL_Surface *target, void *object) {
	RGS_Label *label = (RGS_Label*)object;
	
	label->font->Surface->format->palette->colors[2].r = label->color.r;
	label->font->Surface->format->palette->colors[2].g = label->color.g;
	label->font->Surface->format->palette->colors[2].b = label->color.b;
	

	BFont_SetCurrentFont(label->font);
	BFont_PutString(target, ((RGS_Object*)object)->x, ((RGS_Object*)object)->y, label->text);
}

void RGS_SetLabelText(RGS_Label *label, char *text) {
	BFont_SetCurrentFont(label->font);
	label->object.w = BFont_TextWidth(text);
	
	if(label->text != NULL)
		free(label->text);
		
	label->text = (char*)malloc(strlen(text)+1);
	strcpy(label->text, text);
}